<?php
session_start();


include 'connexion.php';

if(isset($_POST["valider"]))
{
	$nom = $_POST["nom"];
	$prenom = $_POST["prenom"];
	$age = $_POST["annee"]."-".$_POST["mois"]."-".$_POST["jour"];	
	$phone = $_POST["phone"];
	$mail = $_POST['mail'];
	$mdp = sha1('gz' . $_POST["mdp"]);
	$mdp2 = sha1('gz' . $_POST["mdp2"]);

	if( empty($_POST["nom"]) || empty($_POST["prenom"]) || empty($_POST["phone"]) || empty($_POST["mail"]) || empty($_POST["mdp"]) || empty($_POST["mdp2"])) 
	{
		$erreur = "Vous n'avez pas remplie un champs :)";
	}
	else
	{

		if (preg_match("#^[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$#", $mail))
		{
		    $m = 1;
		}
		 else
		{
			$m = 0;
		}
		if (preg_match("#^[0-9]{10}$#", $phone))
		{
		    $p = 1;
		}
		 else
		{
			$p = 0;
		}


			$msql = "SELECT mail FROM user WHERE mail='$mail'";
		    $mreq = mysqli_query($db,$msql) or die('Erreur SQL !'.$msql.'<br>'.mysqli_error($db));
			$mres = mysqli_fetch_array($mreq);
			$mpv = $mres['mail'];

			if ($mdp != $mdp2) 
			{
				$erreur = "Les deux mots de passe ne sont pas identique";
				header('Location: inscription.php');
			}elseif ($mail == $mpv) 
			{
				$erreur = "Ce mail est déjà utilisé";
			}else{
				if ($m == 0) 
				{
					$erreur = "Votre mail n'est pas valide";
				}
				elseif ($p == 0) 
				{
					$erreur = "Votre numéro de telephone n'est pas valide";
				}else{

	    		// ci-dessous on insère les infos dans la table carnet 
	    		$sql = "INSERT INTO user (id, mdp, nom, prenom, mail, statut, phone, age)
			VALUES ('',
				'".$mdp."',
				'".$nom."',
				'".$prenom."',
				'".$mail."',
				'1','".$phone."',
				'".$age."');"; 
	    		mysqli_query($db,$sql) or die('Erreur SQL !'.$sql.'<br>'.mysqli_error($db)); 

	    				$_SESSION['nom']=$nom;
				    	$_SESSION['id_user']=$id_user;
				    	$_SESSION["log"]=1;
	    			if (!empty($_SESSION['url'])) {
				    		header('Location: '.$_SESSION['url'].'');
				    	}
				    	else
				    	{
				    		header('Location: index.php');
				    	}
		mysqli_close($db);  // on ferme la connexion
			}
		}
	}
}

?>

<html>

	<head>
      <link rel="stylesheet" type="text/css" href="style.css"/>
      <meta charset="UTF-8" />
      <meta name="description" content="Free Bed ou comment trouver un endroit pour dormir en 2 cliques."/>
      <title>Free Bed</title>
    </head>

	<body>

		<script type="text/javascript">

			alert("<?php echo $erreur ?>");

		</script>			
		<div id="conteneur">
            <div id="header">
                <?php include_once("header.php"); ?>
        	</div><br/>
            <div id="contenu">
				<div id="inscription">
					<form method="post">
						<table>
							<tr>
								<td>
									<input type="text" id="nom" name="nom" placeholder="Nom"/>
								</td>
							</tr>
							<tr>
								<td>
									<input type="text" id="prenom" name="prenom" placeholder="Prénom"/>
								</td>
							</tr>
							<tr>
								<td>
							<select name="jour">
							    <?php for ($jour = 1 ; $jour <= 31 ; $jour++){ ?>
							    <option value="<?php echo $jour ?>"><?php echo $jour; ?></option>
								<?php } ?>  
							</select>
							<select name="mois">
		        				<?php for ($mois = 1 ; $mois <= 12 ; $mois++){ ?>
							    <option value="<?php echo $mois ?>"><?php echo $mois; ?></option>
								<?php } ?>  
							</select>
							<select name="annee">
							    <?php for ($annee = 2013 ; $annee >= 1900 ; $annee--){ ?>
							    <option value="<?php echo $annee ?>"><?php echo $annee; ?></option>
								<?php } ?>  
							</select>
							</td>
							</tr>
							<tr>
								<td>
									<input type="email" id="mail" name="mail" placeholder="Adresse mail"/>
								</td>
							</tr>
							<tr>
								<td>
									<input type="text" id="phone" name="phone" placeholder="Numéro de Telephone"/>
								</td>
							</tr>
							<tr>
								<td>
									<input type="password" id="mdp" name="mdp" placeholder="Mot de passe"/>
								</td>
							</tr>
							<tr>
								<td>
									<input type="password" id="mdp2" name="mdp2" placeholder="Confirmation du mot de passe"/>
								</td>
							</tr>
							<tr>
								<td colspan="2" align="center">
									<input type="submit" value="Valider" name="valider"/>
								</td>
							</tr>					
						</table>
					</form>
				</div>
			<div id="footer">
                <?php include_once("footer.html"); ?>

            </div>
		</div>
	</body>
</html>